package com.itump.base.util;
import javax.servlet.http.HttpServletRequest;

/**
 
 * <p>功能描述: 初始化</p>
 
 */

public class ReqRespUtils {
    /**
     * <p> Constructor
     * */
    private ReqRespUtils() {
    }

    /**
     * 封装String 的获取
     * @param req  HttpServletRequest
     * @param name String
     * @return String
     */

    public static String getStringValue(HttpServletRequest req,
                                        String name) {
        if (name == null)return "";
        String value = req.getParameter(name);
        if (value == null) value = "";
        return value;
    }

    /**
     * 封装Int 的获取
     * @param req  HttpServletRequest
     * @param name String
     * @return int
     */

    public static int getIntValue(HttpServletRequest req,
                                  String name) {
        if (name == null)return 0;
        String value = req.getParameter(name);
        if (value == null || value.equals(""))
        	return 0;
        else
        	return Utils.parseInt(value);
    }

    /**
     * 返回一个时间类型，如果为空则为null
     * @param req  HttpServletRequest
     * @param name String
     * @return java.util.Date
     * */
    public static java.util.Date getDateValue(HttpServletRequest req,
            								  String name) {
    	if (name == null) return null;
    	String value = req.getParameter(name);
    	if (value == null || value.trim().equals("")) return null;

    	return Formater.strToDate(value);
    }
   
    /**
     * <p> 返回当前页，从1开始，
     * @param HttpServletRequest req,
     * @param page type of String
     * */
    public static int getPage(HttpServletRequest req,
                       String page) throws Exception {
        if (page == null) return 0;
        String page_str = getStringValue(req, page);
        page_str = (page_str.length() == 0) ? "1" : page_str;

        return Utils.parseInt(page_str);
    }

    /**
     * <p> 返回当前的页面的大小
     * @param req type of HttpServletRequest
     * @param pageSize type of String
     * */
    public static int getPageSize(HttpServletRequest req,
                           String pageSize) throws Exception {
        if (pageSize == null) return 0;
        String ps = getStringValue(req, pageSize);
        int page_size = 0;
        if(ps.trim().length() == 0) {
            /* 设置默认的行数 */
            PropertiesReader reader = PropertiesReader.getIntance();
            page_size = reader.getInt("page.size").intValue();
        } else {
            page_size = Utils.parseInt(ps);
        }

        return page_size;
    }

    /**
     * <p> 返回当前的区域语言
     * @param req type of HttpServletRequest
     * @return String, en,zh and so on
     * */
    public static String getLang(HttpServletRequest req) {
    	return (req.getLocale().getLanguage().equals("en")) ? "en" : "zh";
    }

 }
